package com.sxt.opd.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxt.opd.domain.Guahao;
import com.sxt.opd.domain.Jiuzhen;
import com.sxt.opd.domain.Patient;
import com.sxt.opd.service.JiuzhenService;
import com.sxt.opd.service.PatientService;
import com.sxt.opd.vo.GuahaoVo;
import com.sxt.opd.vo.JiuzhenVo;
import com.sxt.sys.common.Constast;
import com.sxt.sys.common.DataGridView;
import com.sxt.sys.common.ResultObj;
import com.sxt.sys.common.WebUtils;
import com.sxt.sys.domain.Dept;
import com.sxt.sys.domain.Permission;
import com.sxt.sys.domain.User;
import com.sxt.sys.service.DeptService;
import com.sxt.sys.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2020-01-21
 */
@RestController
@RequestMapping("/jiuzhen")
public class JiuzhenController {
    

        @Autowired
        private JiuzhenService jiuzhenService;
        @Autowired
        private PatientService patientService;
        @Autowired
        private UserService userService;
        @Autowired
        private DeptService deptService;

    /**
     * 查询
     */
    @RequestMapping("loadAllJiuzhen")
    public DataGridView loadAllGuahao(JiuzhenVo jiuzhenVo) {
        IPage<Jiuzhen> page = new Page<>(jiuzhenVo.getPage(), jiuzhenVo.getLimit());
        QueryWrapper<Jiuzhen> queryWrapper = new QueryWrapper<>();
        if(jiuzhenVo.getJiuzhenstatus()==Constast.JIUZHEN_FINISH){
            queryWrapper.eq("jiuzhenstatus", Constast.JIUZHEN_FINISH);
        }else{
            queryWrapper.lt("jiuzhenstatus", Constast.JIUZHEN_FINISH);
        }
        queryWrapper.eq(jiuzhenVo.getId()!=null&&jiuzhenVo.getId()!=0,"id",jiuzhenVo.getId());
        queryWrapper.eq(jiuzhenVo.getPatientid()!=null&&jiuzhenVo.getPatientid()!=0,"patientid",jiuzhenVo.getPatientid());
        queryWrapper.ge(jiuzhenVo.getStartTime()!=null, "createtime", jiuzhenVo.getStartTime());
        queryWrapper.le(jiuzhenVo.getEndTime()!=null, "createtime", jiuzhenVo.getEndTime());
        queryWrapper.like(StringUtils.isNotBlank(jiuzhenVo.getOperateperson()), "operateperson", jiuzhenVo.getOperateperson());
        queryWrapper.orderByAsc("ordernum");
        this.jiuzhenService.page(page, queryWrapper);
        List<Jiuzhen> records = page.getRecords();
        for (Jiuzhen jiuzhen : records) {
            Patient patient = this.patientService.getById(jiuzhen.getPatientid());
            jiuzhen.setPatient(patient);
            User user = userService.getById(jiuzhen.getUserid());
            jiuzhen.setDoctorname(user.getName());
            Dept dept = this.deptService.getById(user.getDeptid());
            jiuzhen.setMenzhentitle(dept.getTitle());
            jiuzhen.setDeptid(user.getDeptid());
        }
        return new DataGridView(page.getTotal(), records);
    }
        /**
         * 添加
         */
        @RequestMapping("addJiuzhen")
        public ResultObj addJiuzhen(JiuzhenVo jiuzhenVo) {
            try {
                jiuzhenVo.setCreatetime(new Date());//set创建时间
                jiuzhenVo.setGoodslistnum(new Date().getTime()+""+jiuzhenVo.getPatientid());//set药单号
                jiuzhenVo.setOrdernum(loadMenuMaxOrderNum());//set排队号码
                this.jiuzhenService.save(jiuzhenVo);
                return ResultObj.ADD_SUCCESS;
            } catch (Exception e) {
                e.printStackTrace();
                return ResultObj.ADD_ERROR;
            }
        }

        /**
         * 加载最大的排序码
         * @return
         */
        public Integer loadMenuMaxOrderNum(){
            QueryWrapper<Jiuzhen> queryWrapper=new QueryWrapper<>();
            queryWrapper.orderByDesc("ordernum");
            IPage<Jiuzhen> page=new Page<>(1, 1);
            List<Jiuzhen> list = this.jiuzhenService.page(page, queryWrapper).getRecords();
            if(list.size()>0) {
                return list.get(0).getOrdernum()+1;
            }else {
                return 1;
            }
        }

        /**
         * 修改
         */
        @RequestMapping("updateJiuzhen")
        public ResultObj updateJiuzhen(JiuzhenVo jiuzhenVo) {
            try {
                this.jiuzhenService.updateById(jiuzhenVo);
                return ResultObj.UPDATE_SUCCESS;
            } catch (Exception e) {
                e.printStackTrace();
                return ResultObj.UPDATE_ERROR;
            }
        }


        /**
         * 删除
         */
        @RequestMapping("deleteJiuzhen")
        public ResultObj deleteJiuzhen(Integer id){
            try {
                this.jiuzhenService.removeById(id);
                return ResultObj.DELETE_SUCCESS;
            } catch (Exception e) {
                e.printStackTrace();
                return ResultObj.DELETE_ERROR;
            }
        }

    



}

